﻿using InfonavaEye.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye.Forms
{
    public partial class CreateOperatingForm : Form
    {
        public OperationInfo entity = null;

        private OperationInfo operationInfo = null;

        public CreateOperatingForm(OperationInfo operationInfo = null)
        {
            this.operationInfo = operationInfo;
            InitializeComponent();
        }

        private void CreateOperatingForm_Load(object sender, EventArgs e)
        {
            //dateTimePicker1.Format = DateTimePickerFormat.Long;
            if (this.operationInfo!=null) {
                this.dateTimePicker1.Text = this.operationInfo.operatingDate;
                if (this.operationInfo.roomNum!=null) {
                    for (var i=0; i < this.cmbRoom.Items.Count; i++) {
                        var item = this.cmbRoom.Items[i];
                        if (item.ToString().Equals(this.operationInfo.roomNum)) {
                            this.cmbRoom.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnCreateOrSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.dateTimePicker1.Text)) {
                MessageBox.Show("请填写手术日期");
                return;
            }

            if (String.IsNullOrEmpty(this.cmbRoom.Text))
            {
                MessageBox.Show("请选择手术室号码");
                return;
            }

            if (String.IsNullOrEmpty(this.cmbDoctor.Text))
            {
                MessageBox.Show("请选择医生姓名");
                return;
            }


            if (entity==null) {
               
                entity = new OperationInfo()
                {
                    OperationId = Guid.NewGuid().ToString(),
                    operatingDate = this.dateTimePicker1.Text,
                    roomNum = this.cmbRoom.Text,
                    doctorName = this.cmbDoctor.Text,
                    patient = this.textPatient.Text,
                    manufacturers = this.cmbFactory.Text,
                };
                RadioButton opType = getOpType();
                if (opType!=null) {
                    entity.operationType = int.Parse(opType?.Tag.ToString());
                    entity.operationTypeName = opType?.Text.ToString();
                }

                RadioButton opPart = getOpPart();
                if (opPart!=null) {
                    entity.operationPart = int.Parse(opPart?.Tag.ToString());
                    entity.operationPartName = opPart?.Text.ToString();
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private RadioButton getOpType() {
            RadioButton[] group = new RadioButton[] {
                this.radioOpType1,this.radioOpType2,this.radioOpType3,this.radioOpType4
            };
            return group.AsEnumerable().FirstOrDefault(p => p.Checked);
        }

        private RadioButton getOpPart() {
            RadioButton[] group = new RadioButton[] {
                this.radioPart1,this.radioPart2,this.radioPart3
            };
            return group.AsEnumerable().FirstOrDefault(p => p.Checked);
        }

        private void radioPart3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioOpType4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
